﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using Waiter.DataLayer;

namespace Waiter.View
{
    public partial class frmRptSaleNumber : DevComponents.DotNetBar.Office2007Form
    {
        public frmRptSaleNumber()
        {
            InitializeComponent();
        }
        private void fsCheckChange(object sender, EventArgs e)
        {
            this.cmbHourWork.Enabled = this.chkHourWork.Checked;
            this.dtiDay.Enabled = this.chkDay.Checked;
            this.dtiMonth.Enabled = this.chkMonth.Checked;
            this.dtiFrom.Enabled = this.chkFromTo.Checked;
            this.dtiTo.Enabled = this.chkFromTo.Checked;
        }
        private void fsCheckChange2(object sender, EventArgs e)
        {
            this.cmbTable.Enabled = this.chkTable.Checked;
            this.cmbArea.Enabled = this.chkArea.Checked;
        }
        private void frmRptSaleNumber_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.frmRptSaleNumber_Load", "", "");
            DateTime currentDate = DateTime.Now;
            this.dtiDay.Value = currentDate;
            this.dtiMonth.Value = currentDate;
            this.dtiTo.Value = currentDate;
            this.dtiFrom.Value = currentDate;
            this.chkHourWork.Checked = true;
            this.chkAll.Checked = true;
            this.fsCheckChange(null, null);
            this.fsCheckChange2(null, null);
            this.txtTotal.Text = "0";
            fsLoadCmbArea();
            fsLoadCMBHowWork();
            fsLoadCmbTable();
        }
        private void fsLoadCmbTable()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsLoadCmbTable", "", "");
            TableCtrl tableCtrl = new TableCtrl();
            try
            {
                List<TABLE> tableList = tableCtrl.fsGetAllTableList(null);
                this.cmbTable.Items.Clear();
                if (tableList == null)
                {
                    return;
                }

                List<ComboboxItem> listItem = new List<ComboboxItem>();
                for (int i = 0; i < tableList.Count; i++)
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = "Bàn " + tableList[i].TABLE_NAME;
                    item.Value = tableList[i].CODE;
                    listItem.Add(item);
                }
                this.cmbTable.DataSource = listItem;
                this.cmbTable.DisplayMember = CommonConstants.CMB_TEXT;
                this.cmbTable.ValueMember = CommonConstants.CMB_VALUE;
                CommonCtrl.fsCmbSetSelectedIndex(this.cmbTable, 0);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsLoadCmbTable", "", ex.ToString());
            }
        }
        private void fsLoadCMBHowWork()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsLoadCMBHowWork", "", "");
            WorkShiftCtrl workShiftCtrl = new WorkShiftCtrl();
            List<WORK_SHIFT> workShiftList = workShiftCtrl.fsGetWorkShiftList();
            List<ComboboxItem> itemList = new List<ComboboxItem>();
            if (workShiftList == null || workShiftList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < workShiftList.Count; i++)
            {
                ComboboxItem item = new ComboboxItem();
                item.Value = workShiftList[i].CODE;
                item.Text = workShiftList[i].WORK_SHIFT_NAME;

                itemList.Add(item);
            }

            this.cmbHourWork.DataSource = itemList;
            this.cmbHourWork.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbHourWork.ValueMember = CommonConstants.CMB_VALUE;
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbHourWork, 0);
        }

        public void fsLoadCmbArea()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsLoadCmbArea", "", "");
            MasterCtrl masterCtrl = new MasterCtrl();
            masterCtrl.fsLoadCmbGroupList(this.cmbArea, CommonConstants.CAC_KHU_VUC, null);
        }

        private bool fsCheckDateFromTo(Control focusItem)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsCheckDateFromTo", "", "");
            try
            {
                if (this.dtiFrom.Value > this.dtiTo.Value)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Ngày không hợp lệ!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    focusItem.Focus();
                    return false;
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.fsCheckDateFromTo", "", ex.ToString());
            }
            return true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.btnClose_Click", "", "");
            this.Close();
        }

        private void dtiFrom_ValueChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.dtiFrom_ValueChanged", "", "");
            fsCheckDateFromTo(this.dtiFrom);
        }

        private void dtiTo_ValueChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.dtiTo_ValueChanged", "", "");
            fsCheckDateFromTo(this.dtiTo);
        }

        long totalNumber1 = 0;
        long totalNumber2 = 0;
        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.btnSearch_Click", "", "");
            if (!this.fsCheckDateFromTo(this.dtiFrom))
            {
                return;
            }

            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            WorkShiftCtrl workShiftCtrl = new WorkShiftCtrl();
            MasterCtrl masterCtrl = new MasterCtrl();

            if (this.chkHourWork.Checked)
            {
                WORK_SHIFT item = workShiftCtrl.fsFindByCode(CommonCtrl.fsCmbGetSelectedValue(this.cmbHourWork));
                if (item != null)
                {
                    startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, item.START_TIME.Hours, item.START_TIME.Minutes, item.START_TIME.Seconds);
                    endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, item.END_TIME.Hours, item.END_TIME.Minutes, item.END_TIME.Seconds);
                }
            }
            else if (this.chkDay.Checked)
            {
                startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day);
                endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
            }
            else if (this.chkMonth.Checked)
            {
                startDate = new DateTime(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month, 1);
                endDate = new DateTime(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month, DateTime.DaysInMonth(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month), 23, 59, 59);
            }
            else if (this.chkFromTo.Checked)
            {
                startDate = new DateTime(this.dtiFrom.Value.Year, this.dtiFrom.Value.Month, this.dtiFrom.Value.Day);
                endDate = new DateTime(this.dtiTo.Value.Year, this.dtiTo.Value.Month, this.dtiTo.Value.Day, 23, 59, 59);
            }

            int type = 1;
            string typeCode = "";
            if (this.chkAll.Checked)
            {
                type = 1;
            }
            else if (this.chkTable.Checked)
            {
                type = 2;
                typeCode = CommonCtrl.fsCmbGetSelectedValue(this.cmbTable);
            }
            else if (this.chkArea.Checked)
            {
                type = 3;
                typeCode = CommonCtrl.fsCmbGetSelectedValue(this.cmbArea);
            }

            ReportCtrl reportCtrl = new ReportCtrl();
            if (this.superTabMain.SelectedTabIndex == 0)
            {
                totalNumber1 = 0;
                this.dgrMenu.Rows.Clear();
                List<ViewRptSaleNumber> resultList = reportCtrl.fsGetTotalSaleNumber(startDate, endDate, type, typeCode, 1);
                if (resultList == null || resultList.Count == 0)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Không tìm thấy kết quả thống kê!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                this.dgrMenu.Rows.Add(resultList.Count);
                for (int i = 0; i < resultList.Count; i++)
                {
                    this.dgrMenu.Rows[i].Cells["colMenuSTT"].Value = i + 1;
                    this.dgrMenu.Rows[i].Cells["colMenuName"].Value = resultList[i].name;
                    this.dgrMenu.Rows[i].Cells["colMenuGroupName"].Value = masterCtrl.fsGetMasterValue(CommonConstants.NHOM_MON, resultList[i].code);
                    this.dgrMenu.Rows[i].Cells["colMenuTotal"].Value = resultList[i].total;
                    totalNumber1 += resultList[i].total;
                }

                this.txtTotal.Text = totalNumber1.ToString();
            }
            else
            {
                totalNumber2 = 0;
                this.dgrGroup.Rows.Clear();
                List<ViewRptSaleNumber> resultList = reportCtrl.fsGetTotalSaleNumber(startDate, endDate, type, typeCode, 2);
                if (resultList == null || resultList.Count == 0)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Không tìm thấy kết quả thống kê!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                this.dgrGroup.Rows.Add(resultList.Count);
                for (int i = 0; i < resultList.Count; i++)
                {
                    this.dgrGroup.Rows[i].Cells["colGroupSTT"].Value = i + 1;
                    this.dgrGroup.Rows[i].Cells["colGroupName"].Value = masterCtrl.fsGetMasterValue(CommonConstants.NHOM_MON, resultList[i].code);
                    this.dgrGroup.Rows[i].Cells["colGroupTotal"].Value = resultList[i].total;
                    totalNumber2 += resultList[i].total;
                }

                this.txtTotal.Text = totalNumber2.ToString();
            }
        }

        private void superTabMain_SelectedTabChanged(object sender, DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmRptSaleNumber.superTabMain_SelectedTabChanged", "", "");
            this.txtTotal.Text = (this.superTabMain.SelectedTabIndex == 0 ? totalNumber1 : totalNumber2).ToString();
        }
    }
}


